package com.ruoyi.platform.utils.redis;

import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 保存信号量
     * @param key 键
     * @param permits 数量
     * @return
     */
    public boolean setSemaphore(String key , int permits){
        Assert.isTrue(permits>0,"信号量数值不合理");
        RSemaphore getSemaphore = redissonClient.getSemaphore(key);
        boolean setPermitsFlag = getSemaphore.trySetPermits(permits);
        Assert.isTrue(setPermitsFlag,"设置信号量异常");
        return true;
    }

    /**
     * 获取信号量的可用值
     * @param key 键
     * @return
     */
    public int getSemaphoreCount(String key){
        Assert.hasText(key,"获取值异常");
        return redissonClient.getSemaphore(key).availablePermits();
    }

    /**
     * 获取信号量
     * @param key 键
     * @return
     */
    public RSemaphore getSemaphore(String key){
        Assert.hasText(key,"获取值异常");
        return redissonClient.getSemaphore(key);
    }


    public StringRedisTemplate getStringRedisTemplate(){
        return stringRedisTemplate;
    }
    /**
     * 普通缓存放入
     * @param key 键
     * @param value 值
     * @return true成功 false失败
     */
    public boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 递增
     * @param key 键
     * @param delta 要增加几(大于0)
     * @return
     */
    public long incr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递增因子必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /**
     * 递减
     * @param key 键
     * @param delta 要减少几(小于0)
     * @return
     */
    public long decr(String key, long delta) {
        if (delta < 0) {
            throw new RuntimeException("递减因子必须大于0");
        }
        System.out.println("Redis: "+key+"当前库存为"+redisTemplate.opsForValue().get(key));
        return redisTemplate.opsForValue().increment(key,-delta);
    }

    /**
     * 获取redis延迟3秒的数据
     * @param deviceMac
     * @return
     */
    public Map<Object, Object> redisKeyThreeSecondsList(String deviceMac){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdfShort = new SimpleDateFormat("yyyyMMddHHmmss");
        Map<Object, Object> entries;
        Date currDate = new Date();
        System.out.println("时间-------"+currDate);
        Date currDate1 = new Date();
        Date currDate2 = new Date();
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(currDate);
        rightNow.add(Calendar.SECOND,-1);//延迟1秒数据
        currDate=rightNow.getTime();
        String strDate = sdf.format(currDate);

        Calendar rightNow1 = Calendar.getInstance();
        rightNow1.setTime(currDate1);
        rightNow1.add(Calendar.SECOND,-2);//延迟2秒数据
        currDate1=rightNow1.getTime();
        String strDate1 = sdf.format(currDate1);

        Calendar rightNow2 = Calendar.getInstance();
        rightNow2.setTime(currDate2);
        rightNow2.add(Calendar.SECOND,-3);//延迟3秒数据
        currDate2=rightNow2.getTime();
        String strDate2 = sdf.format(currDate2);

        String patternStr = "";
        String patternStr1 = "";
        String patternStr2 = "";
        try {
            patternStr =  "T"+sdfShort.format(sdf.parse(strDate));
            patternStr1 = "T"+sdfShort.format(sdf.parse(strDate1));
            patternStr2 = "T"+sdfShort.format(sdf.parse(strDate2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String key1 = deviceMac+patternStr;
        String key2 = deviceMac+patternStr1;
        String key3 = deviceMac+patternStr2;
        if(stringRedisTemplate.boundHashOps(key1).entries().size()>0){
            entries = stringRedisTemplate.boundHashOps(key1).entries();
        }else if(stringRedisTemplate.boundHashOps(key2).size()>0){
            entries = stringRedisTemplate.boundHashOps(key2).entries();
        }else{
            entries = stringRedisTemplate.boundHashOps(key3).entries();
        }
        return entries;
    }

}
